/**
 * 
 */
package mdb.com.vn.wservices.utils;

import java.math.BigDecimal;
import java.rmi.RemoteException;
import java.util.Date;

import javax.xml.rpc.ServiceException;

import mdb.com.vn.model.FixedDepositCasa;
import mdb.com.vn.ws.t24gold.T24GoldLocator;
import mdb.com.vn.ws.t24gold.T24GoldSoap;
import mdb.com.vn.wservices.T24Locator;
import mdb.com.vn.wservices.T24Soap;

/**
 * @author tu.dod
 * 
 */
public class FDCasaFromT24 {

	public static FixedDepositCasa getFDCASAInfoFromT24(
			FixedDepositCasa fixedDepositCasa, String savingBookNo,
			String accountLockedNo) throws RemoteException, ServiceException {
		T24Soap t24Soap = new T24Locator().getT24Soap();
		//T24GoldSoap t24Soap = new T24GoldLocator().getT24GoldSoap();
		String[][][] fdResult = null;
		String[][][] fdCSResult = null;
		String[][] fdResultCus = null;
		String strmsg = String
				.format("LIST FBNK.AZ.ACCOUNT WITH @ID EQ %s PRINCIPAL INTEREST.RATE LOCAL.REF CUSTOMER CO.CODE VALUE.DATE MATURITY.DATE INPUTTER ALL.IN.ONE.PRODUCT",
						savingBookNo);
		fdResult = t24Soap.JQL2(strmsg);
		String strmsgCasa = String
				.format("LIST FBNK.ACCOUNT WITH @ID EQ %s CUSTOMER CATEGORY WORKING.BALANCE CO.CODE INPUTTER ACCT.CREDIT.INT OPENING.DATE",
						accountLockedNo);
		fdCSResult = t24Soap.JQL2(strmsgCasa);

		if (fdResult.length != 0 && fdCSResult.length != 0) {

			if (fdResult[0][4][0].equals(fdCSResult[0][1][0])) {

				fixedDepositCasa.setSavingbookNo(savingBookNo);
				fixedDepositCasa.setFdAmount(new BigDecimal(fdResult[0][1][0]));
				fixedDepositCasa.setInterestRate(Double
						.parseDouble(fdResult[0][2][0]));
				fixedDepositCasa.setFdTerm(fdResult[0][3][3]);
				fixedDepositCasa.setCustomerId(fdResult[0][4][0]);

				fixedDepositCasa.setBranchCode(fdResult[0][5][0]);
				fixedDepositCasa.setStartDate(WSUtils.stringToDate(
						fdResult[0][6][0], "yyyyMMdd"));
				fixedDepositCasa.setEndDate(WSUtils.stringToDate(
						fdResult[0][7][0], "yyyyMMdd"));
				fixedDepositCasa.setCreatedBy(fdResult[0][8][0]);

				fixedDepositCasa.setAccountLocked(accountLockedNo);
				fixedDepositCasa.setWbBalances(new BigDecimal(
						fdCSResult[0][3][0]));
				fixedDepositCasa.setOpeningDate(WSUtils.stringToDate(
						fdCSResult[0][7][0], "yyyyMMdd"));
				fixedDepositCasa.setCategory(fdCSResult[0][2][0]);
				fixedDepositCasa.setProductCode(fdResult[0][9][0]);
				fixedDepositCasa.setDateCreated(new Date());

				String strmsgcus = String
						.format("LIST FBNK.CUSTOMER WITH @ID EQ %s SHORT.NAME STREET TOWN.COUNTRY SECTOR LEGAL.ID LEGAL.ISS.DATE PROVINCE",
								fixedDepositCasa.getCustomerId());
				fdResultCus = t24Soap.JQL1(strmsgcus);
				fixedDepositCasa.setCustomerName(fdResultCus[0][1]);
				fixedDepositCasa.setAddress(fdResultCus[0][2]
						+ fdResultCus[0][3]);
				fixedDepositCasa.setAccInterestRate(0);

				int count = fdCSResult[0][6].length;
				if (count != 0) {
					String date = fdCSResult[0][6][count - 1];
					String newIDACI = fdCSResult[0][0][0] + "-" + date;
					String[][][] aciResult = t24Soap
							.JQL2(String
									.format("LIST FBNK.ACCOUNT.CREDIT.INT WITH @ID EQ %s CR.INT.RATE",
											newIDACI));
					if (aciResult.length != 0) {
						fixedDepositCasa
								.setAccInterestRate(Double
										.parseDouble(aciResult[0][1][0]
												.equals("") ? "0"
												: aciResult[0][1][0]));
					}
				}
				
				//just test
//				fixedDepositCasa.setProductCode("PROMO-FD1");
//				fixedDepositCasa.setCategory("6001");
//				fixedDepositCasa.setFdAmount(new BigDecimal("1500000000"));
//				fixedDepositCasa.setWbBalances(new BigDecimal("20000000"));
//				fixedDepositCasa
//				.setInterestRate(6.8);
//				fixedDepositCasa.setAccInterestRate(0.5);
//				fixedDepositCasa.setFdTerm("3M");
//				fixedDepositCasa.setStartDate(WSUtils.stringToDate("20131209",
//						"yyyyMMdd"));
//				fixedDepositCasa.setOpeningDate(WSUtils.stringToDate("20131209",
//						"yyyyMMdd"));
				//end
				String productCode = fixedDepositCasa.getProductCode();
				String categoryCode = fixedDepositCasa.getCategory();
				String customerId = fixedDepositCasa.getCustomerId();

				if (customerId.substring(0, 1).equals("2")) {

					fixedDepositCasa.setCustomerType(2);

				}
				if (customerId.substring(0, 1).equals("1")) {
					if (productCode.equals("PROMO-FD1")
							&& categoryCode.equals("6001")) {
						fixedDepositCasa.setCustomerType(1);
						int luckyDraw = WSUtils.CalculateLDRFDCasa(
								fixedDepositCasa.getAccInterestRate(),
								fixedDepositCasa.getInterestRate(),
								fixedDepositCasa.getFdAmount(),
								fixedDepositCasa.getWbBalances(),
								fixedDepositCasa.getFdTerm());

						fixedDepositCasa.setLuckyDraw(luckyDraw);
						fixedDepositCasa.setScCard(luckyDraw / 2);
						fixedDepositCasa.setStartDateLocked(WSUtils
								.stringToDate(fdResult[0][6][0], "yyyyMMdd"));
						fixedDepositCasa.setEndDateLocked(WSUtils.stringToDate(
								fdResult[0][7][0], "yyyyMMdd"));
						fixedDepositCasa.setTermLocked(90);
						fixedDepositCasa.setAmountLocked(new BigDecimal(
								fdCSResult[0][3][0]));
						fixedDepositCasa
								.setBagGift(WSUtils
										.CalculateGiftBag(fixedDepositCasa
												.getScCard()));
						fixedDepositCasa
								.setTrayGift(WSUtils
										.CalculateGiftTray(fixedDepositCasa
												.getScCard()));

					}
				}
			}
		}
		return fixedDepositCasa;
	}
}
